package com.whmskj.xjlhbc.mobile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.mobile.service.IMobileDashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 移动端首页数据聚合接口
 * 提供移动端首页所需的所有数据
 */
@RestController("mobileHomeDashboardController")
@Tag(name = "移动端首页", description = "移动端首页数据聚合接口")
@RequestMapping("/mobile/home")
public class MobileDashboardController {

    @Autowired
    private IMobileDashboardService mobileDashboardService;

    /**
     * 获取移动端首页完整数据
     * 包含本月产量完成情况和车间用电情况
     */
    @GetMapping("/home")
    @Operation(description = "获取移动端首页完整数据")
    public AjaxResult getMobileHomeData() {
        try {
            Map<String, Object> result = mobileDashboardService.getMobileHomeData();
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取本月产量完成情况数据
     * 包含整经、浆纱、织布、整理四个工序的数据
     */
    @GetMapping("/production/completion")
    @Operation(description = "获取本月产量完成情况")
    public AjaxResult getProductionCompletion() {
        try {
            Map<String, Object> result = mobileDashboardService.getProductionCompletion();
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取本月车间用电情况数据
     * 包含总用电、准整车间、织布车间的数据
     */
    @GetMapping("/electricity/usage")
    @Operation(description = "获取本月车间用电情况")
    public AjaxResult getElectricityUsage() {
        try {
            Map<String, Object> result = mobileDashboardService.getElectricityUsage();
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日产量趋势数据
     * 用于绘制产量趋势图
     */
    @GetMapping("/production/trend")
    @Operation(description = "获取每日产量趋势数据")
    public AjaxResult getProductionTrend(
            @RequestParam(required = false) String process,
            @RequestParam(required = false) Integer days) {
        try {
            Map<String, Object> result = mobileDashboardService.getProductionTrend(process, days);
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取每日用电趋势数据
     * 用于绘制用电趋势图
     */
    @GetMapping("/electricity/trend")
    @Operation(description = "获取每日用电趋势数据")
    public AjaxResult getElectricityTrend(
            @RequestParam(required = false) String workshop,
            @RequestParam(required = false) Integer days) {
        try {
            Map<String, Object> result = mobileDashboardService.getElectricityTrend(workshop, days);
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取指定车间的用电数据
     */
    @GetMapping("/electricity/workshop/{workshopName}")
    @Operation(description = "获取指定车间的用电数据")
    public AjaxResult getWorkshopElectricity(@PathVariable String workshopName) {
        try {
            Map<String, Object> result = mobileDashboardService.getWorkshopElectricity(workshopName);
            return AjaxResult.success("查询成功", result);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
}
